package com.artpri.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.artpri.model.Goods;
import com.artpri.model.ShopCart;
import com.artpri.model.vo.ShopCartVo;
import com.artpri.model.vo.SimpleGoods;
import com.artpri.service.ShoppingCartService;
import com.artpri.util.TimeUtil;

/**
 * 购物车 -serviceImpl
 * @author ZhangSi
 *
 */
public class ShoppingCartServiceImpl extends BaseService implements
		ShoppingCartService {

	@Override
	@Transactional
	public Integer addCart(Integer goodsNum, Integer cartNum, Integer userNum) {
		
		Goods good = getGoodsDao().get(Goods.class, goodsNum);
		//检查用户是否已添加该商品
		boolean exists = getShopCartDao().checkShopCartExists(userNum, goodsNum);
		//存在返回 1
		if (exists) {
			return 1;
		}
		
		//不存在进行保存
		ShopCart sc = null;
		if (good != null) {
			sc = new ShopCart();
			sc.setCartName(good.getGoodsName());
			sc.setCartPrice(good.getGoodsPrice());
			sc.setGoodsNum(good.getGoodsNum());
			sc.setUserNum(userNum);
			sc.setCreateTime(TimeUtil.getTimeStamp());
			sc.setCartNum(cartNum == null ? 1 : cartNum);
		}
		
		if (sc != null) {
			getShopCartDao().save(sc);
		}
		
		return 2;
	}

	@Override
	@Transactional
	public List<Integer> addCarts(List<Integer> goodsNums, List<Integer> cartNums, Integer userNum) {
		List<Integer> list = new ArrayList<>();
		
		for (int i=0; i<goodsNums.size(); i++) {
			Integer goodsNum = goodsNums.get(i);
			//检测该商品已存在
			boolean exists = getShopCartDao().checkShopCartExists(userNum, goodsNum);
			//若不存在执行插入
			ShopCart sc = null;
			if (!exists) {
				Goods goods = getGoodsDao().get(Goods.class, goodsNum);
				sc = new ShopCart();
				sc.setCartName(goods.getGoodsName());
				sc.setCartPrice(goods.getGoodsPrice());
				sc.setGoodsNum(goods.getGoodsNum());
				sc.setUserNum(userNum);
				sc.setCreateTime(TimeUtil.getTimeStamp());
				sc.setCartNum(cartNums.get(i) == null ? 1 : cartNums.get(i));
				list.add((Integer) getShopCartDao().save(sc));
			}
		}
		
		return list;
	}
	
	@Override
	@Transactional
	public void deleteCart(List<Integer> cartIds) {
		getShopCartDao().delete(cartIds);
	}

	@Override
	@Transactional
	public void deleteCart(Integer id) {
		getShopCartDao().delete(ShopCart.class, id);
	}
	
	@Override
	@Transactional(readOnly=true)
	public List<ShopCartVo> getShopCartByUser(Integer userNum) {
		return getShopCartVoDao().findShopCartVoByUser(userNum);
	}

	@Override
	@Transactional
	public void updateCart(ShopCart shopCart) {
		shopCart.setCreateTime(TimeUtil.getTimeStamp());
		getShopCartDao().update(shopCart);
	}

	@Override
	@Transactional
	public List<Integer> removeStopSellingGoods(List<Integer> cartIds) {
		List<ShopCart> cartList = null;
		if (cartIds != null && cartIds.size() != 0) {
			cartList = new ArrayList<>();
			for (Iterator<Integer> it = cartIds.iterator(); it.hasNext();) {
				cartList.add(getShopCartDao().get(ShopCart.class, it.next()));
			}
		}
		
		List<Integer> list = null;
		
		if (cartList != null) {
			list = new ArrayList<>();
			for (Iterator<ShopCart> it = cartList.iterator(); it.hasNext();) {
				ShopCart cart = it.next();
				Goods goods = getGoodsDao().get(Goods.class, cart.getGoodsNum());
				if (goods == null || goods.getGoodsSold() == 0) {
					list.add(cart.getCartId());
				}
			}
		}
		
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public List<SimpleGoods> getSimpleGoods(List<Integer> goodsNums) {
		return getSimpleGoodsDao().get(goodsNums);
	}

}
